//+------------------------------------------------------------------+
//|                                                         6666.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

// 输入参数
input ulong      InpMagicNumber = 123456;     // EA魔术编号
input string     InpNoTradeTimeStart = "19:30"; // 不交易时间段开始(服务器时间)
input string     InpNoTradeTimeEnd = "23:30";   // 不交易时间段结束(服务器时间)
input double     InpProfitMultiplier = 2.0;   // 盈利乘数(用于计算平仓目标)
input int        InpMAPeriod1 = 5;            // 均线1周期
input int        InpMAPeriod2 = 10;           // 均线2周期
input int        InpMAPeriod3 = 15;           // 均线3周期
input int        InpMAPeriod4 = 20;           // 均线4周期
input int        InpMAPeriod5 = 25;           // 均线5周期
input int        InpMAPeriod6 = 30;           // 均线6周期
input ENUM_APPLIED_PRICE InpMAAppliedPrice = PRICE_CLOSE; // 均线应用价格
input ENUM_MA_METHOD InpMAMethod = MODE_SMA;  // 均线计算方法

// 全局变量
int maHandles[6];        // 均线指标句柄数组
double maValues[6];      // 均线值数组
datetime lastBarTime = 0;// 最后处理的K线时间
bool filterEnabled = false; // 持仓过滤开关状态
long chartID = 0;       // 图表ID
int buttonId = 1;       // 按钮ID

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   // 打印当前品种、周期和魔术编号
   Print("EA初始化 - 魔术编号: ", InpMagicNumber);
   Print("均线参数: ", InpMAPeriod1, ", ", InpMAPeriod2, ", ", InpMAPeriod3, ", ",
         InpMAPeriod4, ", ", InpMAPeriod5, ", ", InpMAPeriod6);
   Print("盈利乘数: ", InpProfitMultiplier);
   Print("不交易时间段: ", InpNoTradeTimeStart, " 至 ", InpNoTradeTimeEnd);
   
   // 创建按钮
   chartID = ChartID();
   CreateButton();
   
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // 删除按钮
   ObjectDelete(chartID, "FilterButton");
   Print("EA已卸载，魔术编号: ", InpMagicNumber);
}

//+------------------------------------------------------------------+
//| 创建按钮函数                                                     |
//+------------------------------------------------------------------+
void CreateButton()
{
   // 创建按钮对象
   if(!ObjectCreate(chartID, "FilterButton", OBJ_BUTTON, 0, 0, 0))
   {
      Print("创建按钮失败: ", GetLastError());
      return;
   }
   
   // 设置按钮属性
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_XDISTANCE, 25);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_YDISTANCE, 50);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_XSIZE, 200);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_YSIZE, 30);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_CORNER, CORNER_LEFT_UPPER);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_BGCOLOR, clrGreen);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_COLOR, clrWhite);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_BORDER_COLOR, clrBlack);
   ObjectSetString(chartID, "FilterButton", OBJPROP_TEXT, "关，程序正常运行");
   
   // 启用按钮接收点击事件
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_SELECTABLE, false);
   ObjectSetInteger(chartID, "FilterButton", OBJPROP_SELECTED, false);
   
   ChartRedraw(chartID);
}

//+------------------------------------------------------------------+
//| 处理图表点击事件                                                 |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam)
{
   // 检查是否是对象点击事件
   if(id == CHARTEVENT_OBJECT_CLICK)
   {
      // 检查是否点击了我们的按钮
      if(sparam == "FilterButton")
      {
         // 切换开关状态
         filterEnabled = !filterEnabled;
         
         // 更新按钮显示
         if(filterEnabled)
         {
            ObjectSetString(chartID, "FilterButton", OBJPROP_TEXT, "开，盈利后出局");
            ObjectSetInteger(chartID, "FilterButton", OBJPROP_BGCOLOR, clrGray);
         }
         else
         {
            ObjectSetString(chartID, "FilterButton", OBJPROP_TEXT, "关，程序正常运行");
            ObjectSetInteger(chartID, "FilterButton", OBJPROP_BGCOLOR, clrGreen);
         }
         
         ChartRedraw(chartID);
      }
   }
}

//+------------------------------------------------------------------+
//| 检查持仓过滤条件                                                 |
//+------------------------------------------------------------------+
bool CheckPositionFilter()
{
   // 如果过滤关闭，直接返回true继续执行
   if(!filterEnabled) return true;
   
   // 检查当前品种是否有持仓
   int total = PositionsTotal();
   string symbol = Symbol();
   
   for(int i = 0; i < total; i++)
   {
      if(PositionGetSymbol(i) == symbol)
      {
         // 找到当前品种的持仓，返回true继续执行
         return true;
      }
   }
   
   // 没有找到当前品种的持仓，返回false退出OnTick
   return false;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // 检查持仓过滤条件
   if(!CheckPositionFilter()) return;
   // 这里继续原有的OnTick逻辑
   Print("333333");
   // ...
}